package com.zhentao.controller;

import afu.org.checkerframework.checker.igj.qual.I;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.SysUserProductMapper;
import com.zhentao.pojo.Product;
import com.zhentao.pojo.SysUserProduct;
import com.zhentao.service.ProductService;
import com.zhentao.service.SysUserProductService;
import com.zhentao.vo.ShoppingCarDto;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @autho：周明震
 * @date 2025/7/4
 */
@RestController
@RequestMapping("/ShoppingCar")
@RequiredArgsConstructor
public class ShoppingCarController {
    @Autowired
    SysUserProductService sysUserProductService;
    @PostMapping("ShoppingCarAdd")
    public Integer  ShoppingCarAdd(@RequestParam Integer uid,@RequestParam Integer pid,@RequestParam Integer num){
        SysUserProduct sysUserProduct = new SysUserProduct();
        sysUserProduct.setUid(uid);
        sysUserProduct.setNum(num);
        sysUserProduct.setPid(pid);
        boolean b = sysUserProductService.save(sysUserProduct);
        if(b){
            return 200;
        }else {
            return 401;
        }
    }
    @Autowired
    ProductService productService;
    @PostMapping("showShoppingCar")
    public List<ShoppingCarDto> showShoppingCar(@RequestParam Integer uid){
        QueryWrapper<SysUserProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        List<SysUserProduct> list = sysUserProductService.list(wrapper);
        ArrayList<ShoppingCarDto> shoppingCarDtos = new ArrayList<>();
        for (SysUserProduct product : list) {
            ShoppingCarDto shoppingCarDto = new ShoppingCarDto();
            shoppingCarDto.setNum(product.getNum());
            Product product1 = productService.getById(product.getPid());
            shoppingCarDto.setId(product.getId());
            shoppingCarDto.setProduct(product1);
            shoppingCarDtos.add(shoppingCarDto);
            System.err.println(shoppingCarDto);
        }
        return shoppingCarDtos;
    }

    @PostMapping("showShoppingUpdate")
    public Integer showShoppingUpdate(@RequestParam Integer id,@RequestParam Integer b){
        SysUserProduct sysUserProduct = sysUserProductService.getById(id);
        if(b==1){
            sysUserProduct.setNum(sysUserProduct.getNum()+1);
            sysUserProductService.updateById(sysUserProduct);
            return 200;
        }else {
            sysUserProduct.setNum(sysUserProduct.getNum()-1);
            sysUserProductService.updateById(sysUserProduct);
            return 200;
        }
    }
    @PostMapping("ShoppingCarDelete")
    public Integer ShoppingCarDelete(@RequestParam List<Integer> ids){
        boolean b = sysUserProductService.removeBatchByIds(ids);
        if (b){
            return 200;
        }else {
            return 401;
        }
    }

}
